package com.black.knight.chess;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.MenuInflater;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebSettings.ZoomDensity;
import android.webkit.WebView;
import android.widget.ImageButton;
import android.widget.TextView;

import com.black.knight.chess.calls.PublishActivityCall;
import com.black.knight.chess.common.ActivityEnum;
import com.black.knight.chess.model.SahModel;
import com.black.knight.chess.model.SettingsModel;
import com.black.knight.chess.utils.Utils;
import com.facebook.android.Facebook;

public class ProfileActivity extends Activity{

	private ProfileActivity context;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        
    	this.context = this;
    	
    	Utils.setLanguage(SettingsModel.getInstance().getLanguage(), context);
    	
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
        setContentView(R.layout.profile);
        
        setTitle(context.getResources().getString(R.string.my_profile));
        
        final ImageButton signoutbutton = (ImageButton) findViewById(R.id.signoutbutton);
        
        final TextView welcomeText = (TextView) findViewById(R.id.welcomeText);
        final WebView webViewBKC = (WebView) findViewById(R.id.webViewBKC);
        
        webViewBKC.getSettings().setJavaScriptEnabled(true);
        webViewBKC.getSettings().setBuiltInZoomControls(true);
        webViewBKC.getSettings().setDefaultZoom(ZoomDensity.FAR);
        webViewBKC.clearCache(true);
        
        String username = SettingsModel.getInstance().getUsername();
        String password = SettingsModel.getInstance().getPassword();
        
        if(SignInActivity.user != null) {
        	username = SignInActivity.user.getUsername();
        	password = SignInActivity.user.getPassword();
        }
        
        webViewBKC.loadUrl("http://www.bk-chess.com?username=" + username + "&password=" + password);
        
        welcomeText.setText(getResources().getString(R.string.welcome_message) + " " + SettingsModel.getInstance().getLoggedUser().getName());
        
        signoutbutton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
		    	if(SettingsModel.getInstance().getLoggedUser().getType().equals(1)) {
					final Facebook facebook = SahModel.getInstance().facebook;
			    	
			    	try {
						SettingsModel.getInstance().setFacebookToken("");
						SettingsModel.getInstance().setFacebookTokenExpiration(0L);
			    		
			    		facebook.logout(context);
					} catch (Exception e) {
						Utils.displayToastMessage(context, "Error while logging out");
					}		    		
		    	}
				
				finish();
				
	        	PublishActivityCall call = new PublishActivityCall(ActivityEnum.SIGN_OUT, SettingsModel.getInstance().getLoggedUser().getUsername(), SettingsModel.getInstance().getLoggedUser().getName(), "");			    		
	    		SettingsModel.EXECUTOR_SERVICE.execute(call);

				SettingsModel.getInstance().setLoggedUser(null);
				
	        	Utils.displayToastMessage(SahModel.context, getResources().getString(R.string.user_logged_out_successfully));
	        	
	        	MenuInflater inflater = SahModel.context.getMenuInflater();
	        	
	            SahModel.context.menu.clear();
	            
	        	if(SettingsModel.getInstance().getLoggedUser() != null) {
	            	inflater.inflate(R.menu.chess_menu_signout, SahModel.context.menu);
	            } else {
	            	inflater.inflate(R.menu.chess_menu, SahModel.context.menu);
	            }    
			}
		});
    }
    
	@Override
    public void onConfigurationChanged(Configuration newConfig) {
        Utils.setLanguage(SettingsModel.getInstance().getLanguage(), context);
      
      super.onConfigurationChanged(newConfig);
    }
}